From a8d23067bc0eb4d50626caea64fe9c924b66f77a Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Tue, 14 Jul 2015 10:38:46 +0100 Subject: [PATCH] tools/libxl: Fix libxl__carefd_opened() to be more useful with an invalid fd In the case that fd is -1, preserve errno and don't attempt to set CLOEXEC. Note that the implementation can still fail, as it ignores fcntl() errors and may not set CLOEXEC properly. Update the documentation accordingly until it is fixed. Signed-off-by: Andrew Cooper CC: Ian Campbell CC: Ian Jackson CC: Wei Liu Acked-by: Ian Jackson --- Fixing the fnctl() error issue involves more TUITs than I currently have. New in v4 --- tools/libxl/libxl_fork.c | 5 ++++- tools/libxl/libxl_internal.h | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/libxl/libxl_fork.c b/tools/libxl/libxl_fork.c index 44866872b9..024c1e2fb9 100644 --- a/tools/libxl/libxl_fork.c +++ b/tools/libxl/libxl_fork.c @@ -112,9 +112,12 @@ libxl__carefd *libxl__carefd_record(libxl_ctx *ctx, int fd) libxl__carefd *libxl__carefd_opened(libxl_ctx *ctx, int fd) { libxl__carefd *cf = 0; + int saved_errno = errno; - cf = libxl__carefd_record(ctx, fd); + if (fd >= 0) + cf = libxl__carefd_record(ctx, fd); libxl__carefd_unlock(); + errno = saved_errno; return cf; } diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 3f1fed847e..5d3499eaef 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -2189,7 +2189,7 @@ _hidden void libxl__carefd_unlock(void); _hidden libxl__carefd *libxl__carefd_record(libxl_ctx *ctx, int fd); /* Combines _record and _unlock in a single call. If fd==-1, - * still does the unlock, but returns 0. Cannot fail. */ + * still does the unlock, but returns 0. */ _hidden libxl__carefd *libxl__carefd_opened(libxl_ctx *ctx, int fd); /* Works just like close(2). You may pass NULL, in which case it's -- 2.30.2